package com.hlingsoft.kkh.service;


import java.util.List;

import cn.jpush.api.common.resp.APIConnectionException;

import cn.jpush.api.push.model.notification.AndroidNotification;


import org.springframework.stereotype.Service;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;



@Service
public class AdminPushService {


    //c端key
    private final String masterSecretC = "840b47c9f397643824a9e28d";
    private final String appKeyC = "ada9570a6f54968b02ce5908";

    //B端key
    private final String masterSecretB = "d878fc30ca1ce0a60405fe02";
    private final String appKeyB = "1a9a098ec269b8f7fab1a676";


    public static final String SYSTEM = "0";
    public static final String JOB = "1";
//    public static final String NOTICE="2";
//    public static final String DONE="3";


    /**推送
     *
     * @param alert      内容
     * @param registerationIds       udId
     * @param flag                   类型
     * @param toB                    true b端  false c端
     * @param evn                    false  开发环境 true 正式环境
   */
    public void pushToClient(String alert, List<String> registerationIds, String flag, boolean toB,boolean evn) {
        JPushClient jpushClient;
        if (toB) {
            jpushClient = new JPushClient(masterSecretB, appKeyB,evn,60000);
        } else {
            jpushClient = new JPushClient(masterSecretC, appKeyC,evn,60000);
        }

        // For push, all you need do is to build PushPayload object.


        try {
            PushPayload payload = buildPushObject_alias_alert(alert, registerationIds, flag);
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("resule:" + result.isResultOK());

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            e.printStackTrace();

        } catch (Exception e) {
            // Should review the error, and fix the request
            e.printStackTrace();

        }
    }

    //推动main方法
    public void pushToClient(String alert, List<String> registerationIds, String flag, boolean toB,String applicationId,String jobName,String merchantName,String merchantId,String jobId) {
        JPushClient jpushClient;
        if (toB) {
            jpushClient = new JPushClient(masterSecretB, appKeyB,true,60000);
        } else {
            jpushClient = new JPushClient(masterSecretC, appKeyC,true,60000);
        }

        // For push, all you need do is to build PushPayload object.


        try {
            //定义推送对象
            PushPayload payload = buildPushObject_alias_alert(alert, registerationIds, flag,applicationId,jobName,merchantName,merchantId,jobId);

            PushResult result = jpushClient.sendPush(payload);
            System.out.println("resule:" + result.isResultOK());

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            e.printStackTrace();

        } catch (Exception e) {
            // Should review the error, and fix the request
            e.printStackTrace();

        }
    }

    /**
     * 广播推送通知所有用户
     *
     * @param alert
     * @return
     */
    public PushPayload buildPushObject_all_all_alert(String alert) {
        return PushPayload.alertAll(alert);
    }


    /**
     * 多用户用户 推送ios 通知
     *
     * @param registerationIds
     * @param type
     * @return
     */
    public PushPayload buildPushObject_alias_alert(String alert, List<String> registerationIds, String type) {

        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(registerationIds))
                .setNotification(Notification.alert(alert))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert)
                                .addExtra("type", type)

                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(1)
                                .setSound("happy")
                                .addExtra("type", type)
                                .build())
                        .build())
                .build();

    }

    public PushPayload buildPushObject_alias_alert(String alert, List<String> registerationIds, String type,String applicationId,String jobName,String merchantName,String merchantId,String jobId ) {



        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(registerationIds))
                .setNotification(Notification.alert(alert))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(AndroidNotification.newBuilder().setAlert(alert)
                                .addExtra("type", type)
                                .addExtra("applicationId",applicationId)
                                .addExtra("jobName",jobName)
                                .addExtra("merchantName",merchantName)
                                .addExtra("merchantId",merchantId)
                                .addExtra("jobId",jobId)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(1)
                                .setSound("happy")
                                .addExtra("type", type)
                                .addExtra("applicationId",applicationId)
                                .addExtra("jobName",jobName)
                                .addExtra("merchantName",merchantName)
                                .addExtra("merchantId",merchantId)
                                .addExtra("jobId",jobId)
                                .build())
                        .build())
                .build();

    }


    /**
     * 推送通知 和消息
     *
     * @param msgContent
     * @param eventId
     * @return
     */
    public PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(String msgContent, long eventId) {

        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag("e_" + eventId))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .addExtra("type", "1")
                        .build())
                .build();


    }


    /**
     * 推送ios 用户 根据别名
     *
     * @param msgContent
     * @return
     */
    public PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String msgContent, String[] alias, String fromUser) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(alias))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .addExtra("来自于", fromUser)
                        .build())
                .build();
    }

}
